﻿using NWNT.Dal.Database;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NWNT.Dal
{
    public class UnitOfWork : IUnitOfWork, IDisposable
    {
        /// <summary>
        /// The context
        /// </summary>
        private readonly NwnthealthcareEntities nwntContext = new NwnthealthcareEntities();

        /// <summary>
        /// The disposed
        /// </summary>
        private bool disposed = false;

        private IGenericRepository<Permission> permissionRepository;

        private IGenericRepository<Role> roleRepository;

        private IGenericRepository<UserInfo> userInfoRepository;

        private IGenericRepository<UserProfile> userProfileRepository;

        private IGenericRepository<RolePermissionMapping> rolePermissionMappingRepository;

        private IGenericRepository<UserRoleMapping> userRoleMappingRepository;

        private IGenericRepository<Country> countryRepository;

        private IGenericRepository<State> stateRepository;

        private IGenericRepository<UserType> userTypeRepository;

        private IGenericRepository<Prefix> prefixRepository;



        private IGenericRepository<QuestionArea> questionAreaRepository;

        private IGenericRepository<Questionnaire> questionnaireRepository;

        private IGenericRepository<QuestionnaireAreaOptionMapping> questionnaireAreaOptionMappingRepository;

        private IGenericRepository<QuestionnaireOption> questionnaireOptionRepository;

        private IGenericRepository<Quote> quoteRepository;

        private IGenericRepository<StressResult> stressResultRepository;

        private IGenericRepository<StressResultDetail> stressResultDetailRepository;

        private IGenericRepository<MediaType> mediaTypeRepository;

        private IGenericRepository<Media> mediaRepository;


        public IGenericRepository<Permission> PermissionRepository
        {
            get
            {
                if (this.permissionRepository == null)
                {
                    this.permissionRepository = new GenericRepository<Permission>(this.nwntContext);
                }

                return this.permissionRepository;
            }
        }

        public IGenericRepository<Role> RoleRepository
        {
            get
            {
                if (this.roleRepository == null)
                {
                    this.roleRepository = new GenericRepository<Role>(this.nwntContext);
                }

                return this.roleRepository;
            }
        }

        public IGenericRepository<UserInfo> UserInfoRepository
        {
            get
            {
                if (this.userInfoRepository == null)
                {
                    this.userInfoRepository = new GenericRepository<UserInfo>(this.nwntContext);
                }

                return this.userInfoRepository;
            }
        }

        public IGenericRepository<UserProfile> UserProfileRepository
        {
            get
            {
                if (this.userProfileRepository == null)
                {
                    this.userProfileRepository = new GenericRepository<UserProfile>(this.nwntContext);
                }

                return this.userProfileRepository;
            }
        }

        public IGenericRepository<RolePermissionMapping> RolePermissionMappingRepository
        {
            get
            {
                if (this.rolePermissionMappingRepository == null)
                {
                    this.rolePermissionMappingRepository = new GenericRepository<RolePermissionMapping>(this.nwntContext);
                }

                return this.rolePermissionMappingRepository;
            }
        }

        public IGenericRepository<UserRoleMapping> UserRoleMappingRepository
        {
            get
            {
                if (this.userRoleMappingRepository == null)
                {
                    this.userRoleMappingRepository = new GenericRepository<UserRoleMapping>(this.nwntContext);
                }

                return this.userRoleMappingRepository;
            }
        }

        public IGenericRepository<Country> CountryRepository
        {
            get
            {
                if (this.countryRepository == null)
                {
                    this.countryRepository = new GenericRepository<Country>(this.nwntContext);
                }

                return this.countryRepository;
            }
        }

        public IGenericRepository<State> StateRepository
        {
            get
            {
                if (this.stateRepository == null)
                {
                    this.stateRepository = new GenericRepository<State>(this.nwntContext);
                }

                return this.stateRepository;
            }
        }

        public IGenericRepository<UserType> UserTypeRepository
        {
            get
            {
                if (this.userTypeRepository == null)
                {
                    this.userTypeRepository = new GenericRepository<UserType>(this.nwntContext);
                }

                return this.userTypeRepository;
            }
        }

        public IGenericRepository<Prefix> PrefixRepository
        {
            get
            {
                if (this.prefixRepository == null)
                {
                    this.prefixRepository = new GenericRepository<Prefix>(this.nwntContext);
                }

                return this.prefixRepository;
            }
        }

        public IGenericRepository<QuestionArea> QuestionAreaRepository
        {
            get
            {
                if (this.questionAreaRepository == null)
                {
                    this.questionAreaRepository = new GenericRepository<QuestionArea>(this.nwntContext);
                }

                return this.questionAreaRepository;
            }
        }

        public IGenericRepository<Questionnaire> QuestionnaireRepository
        {
            get
            {
                if (this.questionnaireRepository == null)
                {
                    this.questionnaireRepository = new GenericRepository<Questionnaire>(this.nwntContext);
                }

                return this.questionnaireRepository;
            }
        }

        public IGenericRepository<QuestionnaireAreaOptionMapping> QuestionnaireAreaOptionMappingRepository
        {
            get
            {
                if (this.questionnaireAreaOptionMappingRepository == null)
                {
                    this.questionnaireAreaOptionMappingRepository = new GenericRepository<QuestionnaireAreaOptionMapping>(this.nwntContext);
                }

                return this.questionnaireAreaOptionMappingRepository;
            }
        }

        public IGenericRepository<QuestionnaireOption> QuestionnaireOptionRepository
        {
            get
            {
                if (this.questionnaireOptionRepository == null)
                {
                    this.questionnaireOptionRepository = new GenericRepository<QuestionnaireOption>(this.nwntContext);
                }

                return this.questionnaireOptionRepository;
            }
        }

        public IGenericRepository<Quote> QuoteRepository
        {
            get
            {
                if (this.quoteRepository == null)
                {
                    this.quoteRepository = new GenericRepository<Quote>(this.nwntContext);
                }

                return this.quoteRepository;
            }
        }

        public IGenericRepository<StressResult> StressResultRepository
        {
            get
            {
                if (this.stressResultRepository == null)
                {
                    this.stressResultRepository = new GenericRepository<StressResult>(this.nwntContext);
                }

                return this.stressResultRepository;
            }
        }

        public IGenericRepository<StressResultDetail> StressResultDetailRepository
        {
            get
            {
                if (this.stressResultDetailRepository == null)
                {
                    this.stressResultDetailRepository = new GenericRepository<StressResultDetail>(this.nwntContext);
                }

                return this.stressResultDetailRepository;
            }
        }

        public IGenericRepository<MediaType> MediaTypeRepository
        {
            get
            {
                if (this.mediaTypeRepository == null)
                {
                    this.mediaTypeRepository = new GenericRepository<MediaType>(this.nwntContext);
                }

                return this.mediaTypeRepository;
            }
        }

        public IGenericRepository<Media> MediaRepository
        {
            get
            {
                if (this.mediaRepository == null)
                {
                    this.mediaRepository = new GenericRepository<Media>(this.nwntContext);
                }

                return this.mediaRepository;
            }
        }

        public NwnthealthcareEntities Context
        {
            get { return nwntContext; }
        }

        /// <summary>
        /// Saves this instance.
        /// </summary>
        public void Save()
        {
            this.nwntContext.SaveChanges();
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    this.nwntContext.Dispose();
                }
            }

            this.disposed = true;
        }
    }
}
